<template>
    <div>
        <avue-form ref="form" v-model="obj" :option="option"
                   @reset-change="emptytChange"
                   @submit="submit">

        </avue-form>
    </div>
</template>

<script>

    import {uploadPath} from "@/config/env";
    export default {
        data() {
            return {
                obj: {},
                treeData: [],
                isIdFind: true,
                initSuccess: false,
            }
        },
        props: {
            uri: {},
            rowData: {},
        },
        computed: {
            option() {
                return {
                    // size: "small",
                    submitBtn: true,
                    emptyBtn: true,
                    submitText: '提交',
                    emptyText: "关闭",
                    column: [
                        {
                            label: '层级 ',
                            prop: 'hierarchy',
                            type: 'select',
                            disabled: true,
                            labelWidth: 110,
                            dicData: this.dict.get('HIERARCHY'),
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请选择 层级 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '父级 ',
                            prop: 'pid',
                            type: 'tree',
                            labelWidth: 110,
                            span: 20,
                            display: true,
                            dicData: this.treeData,
                            props: {
                                value: "id",
                                label: "name",
                                children: "children"
                            },
                            rules: [{
                                required: true,
                                message: "请选择 父级 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '标题',
                            prop: 'name',
                            labelWidth: 110,
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请输入 标题",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: 'code',
                            prop: 'code',
                            labelWidth: 110,
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请输入 code",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: '前缀',
                            prop: 'prefix',
                            labelWidth: 110,
                            maxlength: 8,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 前缀",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: '时间格式 ',
                            prop: 'timeFormat',
                            type: 'select',
                            labelWidth: 110,
                            filterable:true,
                            dicData: this.dict.get('TIME_FORMAT'),
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请选择 时间格式 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: '初始自增值 ',
                            prop: 'initIncrement',
                            type: 'number',
                            labelWidth: 110,
                            precision: 0,  //保留小数位,
                            min: 0,
                            max: 9999999999999999,
                            step: 1,
                            row: true,
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请输入 初始自增值 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: '当前自增值 ',
                            prop: 'increment',
                            type: 'number',
                            labelWidth: 110,
                            precision: 0,  //保留小数位,
                            min: 0,
                            max: 9999999999999999,
                            step: 1,
                            row: true,
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请输入 当前自增值 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            display: this.obj.hierarchy === 2,
                            label: '自增值位数',
                            prop: 'incrementLen',
                            type: 'number',
                            labelWidth: 110,
                            precision: 0,  //保留小数位,
                            min: 0,
                            max: 16,
                            step: 1,
                            row: true,
                            span: 20,
                            rules: [{
                                required: true,
                                message: "请输入 自增值长度 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '备注',
                            prop: 'remarks',
                            type: 'textarea',
                            labelWidth: 110,
                            maxlength: 256,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 备注",
                                trigger: "blur"
                            }]
                        },
                    ]
                }
            }
        },
        created() {
            this.treeData = JSON.parse(JSON.stringify(this.rowData.pidDic));
            this.treeDisabledHandle(this.treeData, this.rowData.id);
            if (this.isIdFind) {
                this.findId(this.rowData);
            } else {
                this.obj = this.rowData;
                this.initSuccess = true;
            }
        },
        methods: {
            emptytChange() {
                this.$emit('closeDialog', false);
            },
            // 递归处理自己 + 自己的下级ids 不能选
            treeDisabledHandle(children, id) {
                if (children == null) {
                    return
                }
                for (let i = 0; i < children.length; i++) {
                    if (children[i].pid === id || children[i].id === id) {
                        children[i].disabled = true;
                        this.treeDisabledHandle(children[i].children, children[i].id)
                    } else {
                        this.treeDisabledHandle(children[i].children, id);
                    }
                }
            },
            findId(rowData) {
                if (rowData != null && rowData.id != null) {
                    this.crud.get(this.uri.info + "/" + rowData.id).then((res) => {
                         Object.assign(this.obj, res.data.data);
                         this.initSuccess = true;
                    })
                }
            },
            submit(form, done) {
                this.crud.put(this.uri.info + "/" + this.obj.id, this.obj).then((res) => {
                    if (res.data.code == 200) {
                        this.$emit('closeDialog', true,this.obj.id);
                    }
                    done(form);
                }).catch(err => {
                    console.error(err);
                    done(form);
                })
            },
        }
    }
</script>
